<template>
  <editForm
    :formConfig="memberConfig"
    ref="memberForm"
    :formData="memberFormData"
  ></editForm>
</template>

<script>
export default {
  props: {
    currentRow: {
      type: Object,
      default: () => {
        return {};
      },
    },
    currentProp: {
      type: String,
      default: "",
    },
    currentOrg: {
      type: String,
      default: "",
    },
    staff: {
      type: Array,
      default: () => {
        return [];
      },
    },
    staffType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      memberConfig: {
        //录入基本信息
        labelWidth: "55px",
        itemArr: [
          {
            span: 24,
            label: "值班长",
            prop: "leaderName",
            propArr: "leaderUserArr",
            idsStr: "leaderId",
            defaultOrg: "",
            type: "userOnly-select",
            option: [],
            allowCreate: false,
            isHide: true,
            required: false,
            multiple: false,
            methods: (data, formData) => {
              let member = data.map((item, index) => {
                let obj = {
                  staffName: item.name,
                  staffBy: item.id,
                  type: "0",
                };
                return obj;
              });
              this.$refs.memberForm.setFormItem("leaderList", member);
              this.staffChange();
            },
          },
          {
            span: 24,
            label: "值班员",
            prop: "dutyName",
            propArr: "dutyArr",
            idsStr: "dutyId",
            type: "userOnly-select",
            defaultOrg: "",
            option: [],
            allowCreate: false,
            isHide: true,
            required: false,
            multiple: true,
            methods: (data, formData) => {
              let member = data.map((item, index) => {
                let obj = {
                  staffName: item.name,
                  staffBy: item.id,
                  type: "2",
                };
                return obj;
              });
              this.$refs.memberForm.setFormItem("dutyList", member);
              this.staffChange();
            },
          },
          {
            span: 24,
            label: "值守",
            prop: "guardName",
            propArr: "guardArr",
            idsStr: "guardId",
            type: "userOnly-select",
            defaultOrg: "",
            option: [],
            allowCreate: false,
            isHide: true,
            required: false,
            multiple: true,
            methods: (data, formData) => {
              let member = data.map((item, index) => {
                let obj = {
                  staffName: item.name,
                  staffBy: item.id,
                  type: "1",
                };
                return obj;
              });
              this.$refs.memberForm.setFormItem("guardList", member);
              this.staffChange();
            },
          },
          {
            span: 24,
            label: "值班领导",
            prop: "guidanName",
            propArr: "guidanArr",
            idsStr: "guidanId",
            type: "userOnly-select",
            defaultOrg: "",
            option: [],
            allowCreate: false,
            isHide: true,
            required: false,
            multiple: false,
            methods: (data, formData) => {
              let member = data.map((item, index) => {
                let obj = {
                  staffName: item.name,
                  staffBy: item.id,
                  type: "3",
                };
                return obj;
              });
              this.$refs.memberForm.setFormItem("guidanList", member);
              this.staffChange();
            },
          },
          {
            span: 24,
            label: "值班人员",
            prop: "memberName",
            propArr: "memberArr",
            idsStr: "memberId",
            type: "userOnly-select",
            defaultOrg: "",
            option: [],
            allowCreate: false,
            isHide: true,
            required: false,
            multiple: true,
            methods: (data, formData) => {
              let member = data.map((item, index) => {
                let obj = {
                  staffName: item.name,
                  staffBy: item.id,
                  type: "4",
                };
                return obj;
              });
              this.$refs.memberForm.setFormItem("memberList", member);
              this.staffChange();
            },
          },
        ],
      },
      memberFormData: {},
      members: [],
      shiftInfo: {},
    };
  },
  created() {
    this.members = this.staff;
    this.shiftInfo = this.currentRow;
    this.init(this.staff);
  },
  watch: {
    currentOrg: {
      handler(val) {
        this.memberConfig.itemArr.forEach((item) => {
          item.defaultOrg = val;
        });
      },
    },
  },
  methods: {
    init(data) {
      this.memberConfig.itemArr.forEach((item) => {
        item.defaultOrg = this.currentOrg;
      });
      switch (this.staffType) {
        case "0":
          this.memberConfig.itemArr[0].isHide = false;
          this.memberConfig.itemArr[1].isHide = false;
          this.memberFormData = this.echoStaffData(data);
          let leaderOption = this.getOptionData(
            this.memberFormData.leaderName,
            this.memberFormData.leaderId
          );
          let dutyOption = this.getOptionData(
            this.memberFormData.dutyName,
            this.memberFormData.dutyId
          );
          let dutyArr = this.memberFormData.dutyId
            ? this.memberFormData.dutyId.split(",")
            : [];
          setTimeout(() => {
            this.$refs.memberForm.setItemOption("leaderName", leaderOption);
            this.$refs.memberForm.setItemOption("dutyName", dutyOption);
            this.$refs.memberForm.setFormItem(
              "leaderUserArr",
              this.memberFormData.leaderId
            );
            this.$refs.memberForm.setFormItem("dutyArr", dutyArr);
          }, 500);
          break;
        case "1":
          this.memberConfig.itemArr[2].isHide = false;
          this.memberFormData = this.echoStaffData(data);
          let guardOption = this.getOptionData(
            this.memberFormData.guardName,
            this.memberFormData.guardId
          );
          let guardArr = this.memberFormData.guardId
            ? this.memberFormData.guardId.split(",")
            : [];
          setTimeout(() => {
            this.$refs.memberForm.setItemOption("guardName", guardOption);
            this.$refs.memberForm.setFormItem("guardArr", guardArr);
          }, 500);
          break;
        case "2":
          this.memberConfig.itemArr[1].isHide = false;
          this.memberFormData = this.echoStaffData(data);
          let dutyOption1 = this.getOptionData(
            this.memberFormData.dutyName,
            this.memberFormData.dutyId
          );
          let dutyArr1 = this.memberFormData.dutyId
            ? this.memberFormData.dutyId.split(",")
            : [];
          setTimeout(() => {
            this.$refs.memberForm.setItemOption("dutyName", dutyOption1);
            this.$refs.memberForm.setFormItem("dutyArr", dutyArr1);
          }, 500);
          break;
        default:
          this.memberConfig.labelWidth = "70px";
          this.memberConfig.itemArr[3].isHide = false;
          this.memberConfig.itemArr[4].isHide = false;
          this.memberFormData = this.echoStaffData(data);
          let guidanOption = this.getOptionData(
            this.memberFormData.guidanName,
            this.memberFormData.guidanId
          );
          let memberOption = this.getOptionData(
            this.memberFormData.memberName,
            this.memberFormData.memberId
          );
          let memberArr = this.memberFormData.memberId
            ? this.memberFormData.memberId.split(",")
            : [];
          setTimeout(() => {
            this.$refs.memberForm.setItemOption("guidanName", guidanOption);
            this.$refs.memberForm.setItemOption("memberName", memberOption);
            this.$refs.memberForm.setFormItem(
              "guidanArr",
              this.memberFormData.guidanId
            );
            this.$refs.memberForm.setFormItem("memberArr", memberArr);
          }, 500);
          break;
      }
    },
    //回显人员数据
    echoStaffData(data) {
      let obj = {
        leaderList: [],
        dutyList: [],
        guardList: [],
        guidanList: [],
        memberList: [],
      };
      data.forEach((item) => {
        if (item.type == "0") {
          obj.leaderName = item.staffName;
          obj.leaderId = item.staffBy;
          obj.leaderList.push(item);
        } else if (item.type == "1") {
          if (obj.guardName == undefined) {
            obj.guardName = item.staffName;
            obj.guardId = item.staffBy;
          } else {
            obj.guardName += "," + item.staffName;
            obj.guardId += "," + item.staffBy;
          }
          obj.guardList.push(item);
        } else if (item.type == "2") {
          if (obj.dutyName == undefined) {
            obj.dutyName = item.staffName;
            obj.dutyId = item.staffBy;
          } else {
            obj.dutyName += "," + item.staffName;
            obj.dutyId += "," + item.staffBy;
          }
          obj.dutyList.push(item);
        } else if (item.type == "3") {
          if (obj.guidanName == undefined) {
            obj.guidanName = item.staffName;
            obj.guidanId = item.staffBy;
          } else {
            obj.guidanName += "," + item.staffName;
            obj.guidanId += "," + item.staffBy;
          }
          obj.guidanList.push(item);
        } else {
          if (obj.memberName == undefined) {
            obj.memberName = item.staffName;
            obj.memberId = item.staffBy;
          } else {
            obj.memberName += "," + item.staffName;
            obj.memberId += "," + item.staffBy;
          }
          obj.memberList.push(item);
        }
      });
      return obj;
    },
    getOptionData(nameStr, idStr) {
      let nameArr = nameStr ? nameStr.split(",") : [];
      let idArr = idStr ? idStr.split(",") : [];
      let option = [];
      nameArr.map((i, index) => {
        let item = {
          label: i,
          value: idArr[index],
          id: idArr[index],
          name:i
        };
        option.push(item);
      });
      return option;
    },
    //确定选择
    async staffChange() {
      try {
        let data = [];
        let params = await this.$refs.memberForm.getFormData();
        data = data.concat(params.leaderList);
        data = data.concat(params.dutyList);
        data = data.concat(params.guardList);
        data = data.concat(params.guidanList);
        data = data.concat(params.memberList);
        let members = data.map((item) => item.staffBy);
        if (data.length == 0) {
          this.$message.warning("值班人员不能为空");
        } else if (new Set(members).size != members.length) {
          this.$message.warning("值班人员不能重复，请重新选择");
        } else {
          this.members = data;
          this.shiftInfo[this.currentProp] = data;
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>